/*
 * Copyright (c) 2011 by Danny Backx
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This application shows some facts about the system, and presents
 * buttons to pop up the Development Settings window, and to quit.
 * 
 * The data presented is also saved on /sdcard/showVersions.txt ,
 * or a similar path (the directory is system-dependent).
 */

package info.backx.showVersions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShowVersionsActivity extends Activity {
    Activity thiz;
    String apkFilePath;
    PackageManager pm;
    File dir;
    BufferedWriter of;
    ApplicationInfo ai;
    
    private void AddHeader(String hdr) {
        LinearLayout v = (LinearLayout) findViewById(R.id.verticalLayout);
        TextView child = new TextView(this);
        
        // Hard coded red foreground, gray background
        child.setTextColor(0xFFFF0000);
        child.setBackgroundColor(0xFF222222);
        
        child.setText(hdr);
        v.addView(child);
        
        if (of != null) {
        	try {
        		of.append(hdr);
        		of.append('\n');
    		} catch (IOException e) {
    		}
        }
    }
    
    private void AddText(String hdr, String value) {
    	String txt = hdr + " : " + value;
        LinearLayout v = (LinearLayout) findViewById(R.id.verticalLayout);
        TextView child = new TextView(this);
        child.setText(txt);
        v.addView(child);
   
        if (of != null) {
        	try {
				of.append(txt);
				of.append('\n');
			} catch (IOException e) {
			}
        }
    }
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        thiz = this;
        pm = getPackageManager();
		dir = getFilesDir();
		File sdcard = Environment.getExternalStorageDirectory();
		try {
			of = new BufferedWriter(new FileWriter(new File(sdcard.getAbsoluteFile(), "showVersions.txt")));
		} catch (IOException e3) {
			e3.printStackTrace();
			of = null;
		}

		try {
			ai = pm.getApplicationInfo("info.backx.showVersions", 0);
	        apkFilePath = ai.sourceDir;
		} catch (NameNotFoundException e1) {
			e1.printStackTrace();
			apkFilePath = null;
		}

        /*
         * Show a number of hardware and software facts about the system
         */
        AddHeader("Hardware");
        AddText("Board", Build.BOARD);
        AddText("CPU ABI", Build.CPU_ABI);
        AddText("Brand", Build.BRAND);
        AddText("Device", Build.DEVICE);
        AddText("Display", Build.DISPLAY);
        AddText("Fingerprint", Build.FINGERPRINT);
        AddText("ID", Build.ID);
        AddText("Manufacturer", Build.MANUFACTURER);
        AddText("Model", Build.MODEL);
        AddText("Product", Build.PRODUCT);
        // AddText("Serial", Build.SERIAL); // documented but apparently unavailable
        // AddText("Radio", Build.getRadioVersion()); // documented but apparently unavailable

        AddHeader("Software");
        AddText("SDK", Integer.toString(Build.VERSION.SDK_INT));
        // AddText("SDK String", Build.VERSION.SDK); // deprecated, and same as SDK_INT
        AddText("SDK Code name", Build.VERSION.CODENAME);
        AddText("SDK Incremental", Build.VERSION.INCREMENTAL);
        AddText("SDK Release", Build.VERSION.RELEASE);
        AddText("Tags", Build.TAGS);
        AddText("Type", Build.TYPE);
        AddText("User", Build.USER);
        AddText("Host", Build.HOST);
        
        AddHeader("File system");
		AddText("FilesDir", dir.getAbsolutePath());
		AddText("APK FilePath", (apkFilePath == null) ? "?" : apkFilePath);
		AddText("External storage", sdcard.getAbsolutePath());
		
		AddHeader("Features");
		FeatureInfo[] saf = pm.getSystemAvailableFeatures();
		for (FeatureInfo f : saf) {
			AddText("Feature", f.name);
		}
		
		if (of != null) {
			try {
				of.close();
				of = null;
		        AddHeader("Logging");
				AddText("Log saved in", sdcard.getAbsolutePath() + "/showVersions.txt");
			} catch (IOException e) {
				//
			}
		}
		
        /* Make the quit button work (really terminates the app). */
        Button quitButton = (Button)findViewById(R.id.quitButton);
        quitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				thiz.finish();
			}	
        });

		/*
		 * Pop up the debugging screen that is hidden from Settings by the
		 * manufacturer on some tablets.
		 */
        Button debugButton = (Button)findViewById(R.id.debugButton);
        debugButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		        try {
		        	Intent i = new Intent();
		        	i.setClassName("com.android.settings", "com.android.settings.DevelopmentSettings");
		        	startActivity(i);
		        } catch (Exception e) {
		        	Log.e("ShowVersions", e.getMessage());
		        	e.printStackTrace();
		        }					
			}
        });
    }
}